package com.star.main;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.star.common.CommonApp;
import com.star.dao.CityDao;
import com.star.dao.KeyNewsDao;
import com.star.dao.NewsDao;
import com.star.dao.WeatherDao;
import com.star.dao.WeixinDao;
import com.star.model.CityModel;
import com.star.model.KeyNewsModel;
import com.star.model.NewsModel;
import com.star.model.WeathModel;
import com.star.model.WeatherData;
import com.star.model.WeatherDo;
import com.star.model.WeixinModel;
import com.star.model.WeixinUtilsDo;
import com.star.utils.NewsDataUtils;
import com.star.utils.PostMethod;
import com.star.utils.WeatherDataUtils;
import com.star.utils.WeixinDataUtils;

@Service("newsApp")
public class NewsApp implements CommonApp {

	protected transient final static Logger logger = Logger.getLogger(NewsApp.class);

	@Autowired
	private KeyNewsDao keyNewsDao;

	@Autowired
	private NewsDao newsDao;

	@Autowired
	private WeixinDao weixinDao;

	@Autowired
	private CityDao cityDao;

	@Autowired
	private WeatherDao weatherDao;

	private static Integer count = 0;

	@PostConstruct
	@Override
	public void init() {
		logger.info("-----------------拉取新闻任务开始执行------------------");
	}

	@Override
	public void execute() {

	}
	
	//每3小时执行一次
    @Scheduled(fixedDelay = 1000 * 60 * 180)
	public void pullWeath() {
		List<CityModel> cityList = cityDao.queryCityList();
		if (cityList != null && cityList.size() > 0) {
			for (CityModel city : cityList) {
				Map<String, Object> query = new HashMap<String, Object>();
				query.put("cityname", city.getDistrict());
				String result = WeatherDataUtils.getWeatherUtils("http://op.juhe.cn/onebox/weather/query",
						PostMethod.GET, query);
				WeatherData weaData = JSON.parseObject(result, WeatherData.class);
				WeatherData weatherData = JSON.parseObject(weaData.getData(), WeatherData.class);
				WeathModel model = new WeathModel();
				model.setLife(weatherData.getLife());
				model.setPm25(weatherData.getPm25());
				model.setWeather(weatherData.getWeather());
				WeatherDo weatherDo = JSON.parseObject(weatherData.getRealtime(), WeatherDo.class);
				model.setCityCode(weatherDo.getCity_code());
				model.setCityName(weatherDo.getCity_name());
				model.setDate(weatherDo.getDate());
				model.setStime(weatherDo.getTime());
				model.setWeek(weatherDo.getWeek());
				model.setMoon(weatherDo.getMoon());
				model.setDataUptime(weatherDo.getDataUptime());
				WeatherDo windDo = JSON.parseObject(weatherDo.getWind(), WeatherDo.class);
				model.setDirect(windDo.getDirect());
				model.setWindspeed(windDo.getWindspeed());
				model.setPower(windDo.getPower());
				WeatherDo temp = JSON.parseObject(weatherDo.getWeather(), WeatherDo.class);
				model.setTemperature(temp.getTemperature());
				model.setInfo(temp.getInfo());
				model.setImg(temp.getImg());
				model.setHumidity(temp.getHumidity());
				weatherDao.insertWeather(model);
			}
		}
		logger.info("*********************天气记录拉取成功******************");
	}

	@Scheduled(fixedDelay = 1000 * 60 * 10)
	public void pullWeixin() {
		Integer count = 1;
		while (true) {
			Map<String, Object> query = new HashMap<String, Object>();
			query.put("pno", count);
			query.put("ps", 10);
			String list = WeixinDataUtils.getWeixinUtils("http://v.juhe.cn/weixin/query", PostMethod.GET, query);
			WeixinUtilsDo weixinDo = JSON.parseObject(list, WeixinUtilsDo.class);
			String[] params = weixinDo.getList();
			if (params.length > 0) {
				for (String model : params) {
					WeixinModel weixinModel = JSON.parseObject(model, WeixinModel.class);
					String temp = weixinDao.queryWeixin(weixinModel.getId());
					if (temp == null) {
						weixinDao.insertWeixin(weixinModel);
					}
				}
			}
			if (weixinDo.getPno() <= count) {
				break;
			}
			count++;
		}
		logger.info("微信新增完成***********");
	}

	// 每10分钟执行一次
	@Scheduled(fixedDelay = 1000 * 60 * 10)
	public void pullNews() {
		List<String> keyNewsList = NewsDataUtils.getNewsUtils("http://op.juhe.cn/onebox/news/words", PostMethod.GET,
				null);
		logger.info("实时热点：" + JSON.toJSONString(keyNewsList));
		if (keyNewsList != null && keyNewsList.size() > 0) {
			for (String content : keyNewsList) {
				KeyNewsModel model = keyNewsDao.queryContent(content);
				if (model == null) {
					count++;
					KeyNewsModel keyNewsModel = new KeyNewsModel();
					keyNewsModel.setContent(content);
					keyNewsDao.createKeyNews(keyNewsModel);
					List<String> newsList = NewsDataUtils.getNewsUtils("http://op.juhe.cn/onebox/news/query",
							PostMethod.POST, content);
					if (newsList != null && newsList.size() > 0) {
						for (String news : newsList) {
							NewsModel newModel = JSON.parseObject(news, NewsModel.class);
							KeyNewsModel tempModel = keyNewsDao.queryContent(content);
							newModel.setKeyNewsId(tempModel.getId());
							newsDao.createNews(newModel);
						}
					}
				}
			}
			logger.info("成功插入数据：" + count);
		}
	}

}
